<template>
	<view class="content">
		<view class="add-box">
			<u-form :model="form" ref="uForm">
				<u-form-item label-width="150" label="车队名称">
					<u-input v-model="form.name" placeholder="请输入车队名称" />
				</u-form-item>
				<u-form-item label-width="150" label="车队负责人">
					<u-input v-model="contact" @click="selectShow = true" placeholder="请输选择车队负责人" type="select" />
				</u-form-item>
<!-- 				<u-form-item :border-bottom="false" label="上传图片" prop="photo" label-width="150">
					<u-upload width="160" height="160"></u-upload>
				</u-form-item> -->
			</u-form>
		</view>
		<view class="button-box">
			<u-button type="primary" @tap="addCarTeam" :disabled="isclick">添加</u-button>
		</view>
		<view class="nav" v-show="isclick">
			<navigator url="/pages/project/user/addUser/index" hover-class="navigator-hover">
				<view>没有车队负责人？前往添加 &nbsp; <u-icon name="arrow-right"></u-icon></view>
			</navigator>
		</view>
		<u-select mode="single-column" :list="selectList" v-model="selectShow" @confirm="selectConfirm"></u-select>
	</view>
</template>

<script>
	import {
		getUserInfo
	} from '@/api/user.js'
	import {
		createTeam
	} from '@/api/motorcade.js'
	export default {
		data() {
			return {
				form: {
					name: '',
					projectId: null,
					contactId: null,
				},
				selectList: [],
				selectShow: false,
				contact: '',
				isclick: false,
				selectList: [{
						value: '郑飞',
						label: '郑飞'
					},
					{
						value: '郑飞',
						label: '郑飞'
					},
					{
						value: '郑飞',
						label: '郑飞'
					}
				]
			};
		},
		created() {
			this.getCarUserData()
		},
		onShow() {
			var pages = getCurrentPages();
			this.prevPage = pages[pages.length - 1];
			if (this.prevPage.brand === true) {
				this.getCarUserData()
			}
		},
		methods: {
			// 选择商品类型回调
			selectConfirm(e) {
				e.map((val, index) => {
					this.form.contactId = val.value.id
					this.contact = val.label
					// console.log(val)
				});
			},
			getCarUserData() {
				this.selectList = []
				getUserInfo({roleId: 3}).then(res => {
					console.log(res)
					if (res.code !== 1) {
						uni.showToast({
							title: "数据获取失败",
							icon: "none"
						})
						return
					}
					if(res.result.length === 0){
						// console.log("没有东西")
						this.isclick = true
						// uni.showModal({
						// 	title: "提示",
						// 	content: "未检测到车队负责人，请添加",
						// 	showCancel: false,
						// 	success: res =>{
						// 		if(res.confirm){
						// 			uni.navigateTo({
						// 				url:'/pages/project/user/addUser/index'
						// 			})
						// 		}
						// 	}
						// })
						return
					}
					for (let index in res.result) {
						let lemp = {}
						lemp.label = res.result[index].name
						lemp.value = {}
						lemp.value.id = res.result[index].id
						this.selectList.push(lemp)
					}
					// this.dataList = res.result
					// console.log(this.selectList)
				})
			},
			addCarTeam() {
				this.form.projectId = uni.getStorageSync('id')
				createTeam(this.form).then(res => {
					// console.log(res)
					if (res.code !== 1) {
						uni.showToast({
							title: "数据获取失败",
							icon: "none"
						})
						return
					}
					uni.showToast({
						title: res.msg,
						icon: "none"
					})

					setTimeout(() => {
						var item = true; //item为要传的值
						var pages = getCurrentPages();
						var prevPage = pages[pages.length - 2];
						prevPage.brand = item;
						uni.navigateBack()
					}, 500)


				})
				console.log(this.form)
			}
		}
	};
</script>

<style lang="scss" scoped>
	.content {
		padding: 40rpx;

		.add-box {
			background-color: #ffffff;
			border-radius: 10rpx;
			padding: 20rpx;
		}

		.button-box {
			margin-top: 80rpx;
		}
	}
	.nav{
		text-align: center;
		margin-top: 100rpx;
		color: blue;
	}
</style>
